package client;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client_Chat {
    public void Chat() throws IOException {
        try {
            Socket sk = new Socket("localhost", 3200);
            //System.out.println(sk.getPort());

            InputStream is = sk.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            OutputStream os = sk.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));

            System.out.print("Client: ");
            do {
                Scanner sn = new Scanner(System.in);
                String sentMessage = sn.nextLine();
                bw.write(sentMessage);
                bw.newLine();
                bw.flush();
                // Client muon cham dut viec chat thi go "quit"
                if (sentMessage.equalsIgnoreCase("quit")) {
                    break;
                }
                else {
                    String receivedMessage = br.readLine();
                    System.out.print("Server: " + receivedMessage + "\nClient: ");
                }
            } while (true);

            bw.close();
            br.close();
            sk.close();
        } catch (IOException e) { }
    }
}